package File;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@WebServlet("/download")
public class downLoad extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        String Filename = req.getParameter("filename");
        String folder = "/download/";
        resp.addHeader("Content-Type","application/octet-stream");
        resp.addHeader("Content-Disposition","attachment;filename="+Filename);
        InputStream inputStream = getServletContext().getResourceAsStream(folder+Filename);
        OutputStream outputStream = resp.getOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = inputStream.read(bytes)) != -1){
            outputStream.write(bytes,0,len);
        }
    }
}
